package com.project.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.FacilityBean;
import com.project.bean.RoomBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FacilityServiceImpl implements IFacilityService {

    @Autowired
    private IFacilityMapper iFacilityMapper;
    @Autowired
    private IRoomMapper roomMapper;

    @Override
    public void update(Integer facilityId, Integer roomId) {
        FacilityBean facilityBean = iFacilityMapper.selectById(facilityId);
        facilityBean.setState("已解决");
        iFacilityMapper.updateById(facilityBean);

        QueryWrapper<FacilityBean> qw = new QueryWrapper<>();
        qw.eq("f_state", "未解决");
        qw.eq("fk_roomId", roomId);
        Integer facilityNum = iFacilityMapper.selectCount(qw);
        if (facilityNum == 0) {
            RoomBean roomBean = roomMapper.selectById(roomId);
            roomBean.setState("正常");
            roomMapper.updateById(roomBean);
        }
    }

    @Override
    public void add(FacilityBean facility) {
        iFacilityMapper.insert(facility);
        RoomBean roomBean = roomMapper.selectById(facility.getRoomId());
        roomBean.setState("设施损坏");
        roomMapper.updateById(roomBean);
    }

    @Override
    public List<FacilityBean> findByRoomId(int roomId) {
        QueryWrapper<FacilityBean> qw = new QueryWrapper<>();
        qw.eq("fk_roomId", roomId);
        return iFacilityMapper.selectList(qw);
    }
}
